using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

namespace Code5Systems.FormMaster.WebService
{

	public class FormDataServiceBO
	{
		
		public DataSet GetFormPosts(int formDataId, string moduleIds, DateTime dtStart, DateTime dtEnd)
		{
			
			DataSet ds = null;
			
			try
			{
				ds = GetDataSetFromFormResults(GetFormResults(formDataId, moduleIds, dtStart, dtEnd));
			}
			catch (Exception e)
			{
				throw (e);
			}
			
			return ds;
			
		}
		
		private string GetFormResults(int formDataId, string moduleIds, DateTime dtStart, DateTime dtEnd)
		{
			SqlDataReader dr = default(SqlDataReader);
			
			System.Text.StringBuilder sbXml = new System.Text.StringBuilder(50000);
			
			
			dr = (SqlDataReader) (Code5Systems.FormMaster.Data.DataProvider.Instance().GetWsFormData(formDataId, moduleIds, dtStart, dtEnd));
			
			while (dr.Read())
			{
				
				sbXml.Append(dr.GetValue(0));
			}
			
			if (sbXml.Length == 0)
			{
				throw (new ArgumentException("NoDataAvailable"));
			}
			
			dr.Close();
			
			return sbXml.ToString();
			
		}
		
		
		private DataSet GetDataSetFromFormResults(string xml)
		{
			
			XslCompiledTransform oTransDoc = default(XslCompiledTransform);
			XsltArgumentList oXsltArg = default(XsltArgumentList);
			StringWriter strStream = default(StringWriter);
			NameTable nt = new NameTable();
			
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
			nsmgr.AddNamespace("", "");
			
			bool bHaveData = true;
			
			//Create the XmlParserContext.
			XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
			
			oTransDoc = new XslCompiledTransform();
			
			oXsltArg = new XsltArgumentList();
			
			oTransDoc.Load(HttpContext.Current.Server.MapPath("assets/xslt/FormResultsDataSet.xslt"));
			
			strStream = new StringWriter();
			
			oTransDoc.Transform(new XmlTextReader(xml, XmlNodeType.Document, context), oXsltArg, XmlWriter.Create(strStream), null);
			
			XmlTextReader reader = new XmlTextReader(strStream.ToString(), XmlNodeType.Document, context);
			
			strStream.Close();
			
			DataSet ds = new DataSet();
			
			ds.ReadXml(reader);
			
			if (ds.Tables.Count > 0)
			{
				
				DataTable dt = default(DataTable);
				DataColumn dc = default(DataColumn);
				dt = ds.Tables[0];
				if (dt.Rows.Count > 0)
				{
					
					foreach (DataColumn tempLoopVar_dc in dt.Columns)
					{
						
						dc = tempLoopVar_dc;
						if (dc.ColumnName == "Submitted_By")
						{
							string strSubmittedBy = "SubmittedBy";
							strSubmittedBy = Code5Systems.FormMaster.Utility.GetValidXmlSafeString(strSubmittedBy);
							dc.ColumnName = strSubmittedBy;
						}
						
						dc.ColumnName = (string) (dc.ColumnName.Replace("_", " ").Trim((char) 0));
					}
					
				}
				else
				{
					
					bHaveData = false;
				}
				
			}
			else
			{
				
				bHaveData = false;
				
			}
			
			if (!bHaveData)
			{
				throw (new ArgumentException("NoDataAvailable"));
			}
			
			return ds;
			
		}
		
	}
	
}
